package org.linlinjava.litemall.brand.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.brand.annotation.LoginUser;
import org.linlinjava.litemall.brand.dto.UserInfo;
import org.linlinjava.litemall.brand.service.UserTokenManager;
import org.linlinjava.litemall.core.util.JacksonUtil;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.util.bcrypt.BCryptPasswordEncoder;
import org.linlinjava.litemall.db.domain.LitemallBrand;
import org.linlinjava.litemall.db.domain.LitemallCommodity;
import org.linlinjava.litemall.db.domain.LitemallCustomer;
import org.linlinjava.litemall.db.service.LitemallBrandService;
import org.linlinjava.litemall.db.service.LitemallCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.linlinjava.litemall.brand.util.BrandResponseCode.AUTH_INVALID_ACCOUNT;

/**
 * 鉴权服务
 */
@RestController
@RequestMapping("/brand/auth")
@Validated
public class BrandAuthController {
    private final Log logger = LogFactory.getLog(BrandAuthController.class);

    @Autowired
    private LitemallBrandService brandService;
    @Autowired
    private LitemallCustomerService customerService;
    /**
     * 账号登录
     *
     * @param body    请求内容，{ username: xxx, password: xxx }
     * @param request 请求对象
     * @return 登录结果
     */
    @PostMapping("login")
    public Object login(@RequestBody String body, HttpServletRequest request) {
        String username = JacksonUtil.parseString(body, "username");
        String password = JacksonUtil.parseString(body, "password");
        if (username == null || password == null) {
            return ResponseUtil.badArgument();
        }

        List<LitemallBrand> brandList = brandService.queryByUsername(username);
        LitemallBrand brand = null;
        if (brandList.size() > 1) {
            return ResponseUtil.serious();
        } else if (brandList.size() == 0) {
            return ResponseUtil.fail(AUTH_INVALID_ACCOUNT, "账号不存在");
        } else {
             brand= brandList.get(0);
        }

        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        if (!encoder.matches(password, brand.getPassword())) {
            return ResponseUtil.fail(AUTH_INVALID_ACCOUNT, "账号密码不对");
        }
        // userInfo
        UserInfo userInfo = new UserInfo();
        userInfo.setNickName(username);
        userInfo.setBrandId(brand.getId());
        // token
        String token = UserTokenManager.generateToken(brand.getId());
        Map<Object, Object> result = new HashMap<Object, Object>();
        //result.put("sessionId",session.getId());
        result.put("token", token);
        result.put("userInfo", userInfo);
        return ResponseUtil.ok(result);
    }

}
